home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / BARSDI.PAK / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.1 KB  |  149 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995 Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   misc.c
  9. //
  10. //  PURPOSE:  Contains all helper functions "global" to the application.
  11. //
  12. //  FUNCTIONS:
  13. //    CenterWindow - Center one window over another.
  14. //    CmdStub      - Handles unimplemented commands. 
  15. //                   Demonstrates the statusbar updating. 
  16. //
  17. //  COMMENTS:
  18. //
  19.  
  20. #include <windows.h>            // required for all Windows applications
  21. #include <windowsx.h>
  22. #include "globals.h"            // prototypes specific to this application
  23. #include "statbar.h"
  24.  
  25.  
  26. //
  27. //  FUNCTION: CenterWindow(HWND, HWND)
  28. //
  29. //  PURPOSE:  Center one window over another.
  30. //
  31. //  PARAMETERS:
  32. //    hwndChild - The handle of the window to be centered.
  33. //    hwndParent- The handle of the window to center on.
  34. //
  35. //  RETURN VALUE:
  36. //
  37. //    TRUE  - Success
  38. //    FALSE - Failure
  39. //
  40. //  COMMENTS:
  41. //
  42. //    Dialog boxes take on the screen position that they were designed
  43. //    at, which is not always appropriate. Centering the dialog over a
  44. //    particular window usually results in a better position.
  45. //
  46.  
  47. BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
  48. {
  49.     RECT    rcChild, rcParent;
  50.     int     cxChild, cyChild, cxParent, cyParent;
  51.     int     cxScreen, cyScreen, xNew, yNew;
  52.     HDC     hdc;
  53.  
  54.     // Get the Height and Width of the child window
  55.     GetWindowRect(hwndChild, &rcChild);
  56.     cxChild = rcChild.right - rcChild.left;
  57.     cyChild = rcChild.bottom - rcChild.top;
  58.  
  59.     // Get the Height and Width of the parent window
  60.     GetWindowRect(hwndParent, &rcParent);
  61.     cxParent = rcParent.right - rcParent.left;
  62.     cyParent = rcParent.bottom - rcParent.top;
  63.  
  64.     // Get the display limits
  65.     hdc = GetDC(hwndChild);
  66.     cxScreen = GetDeviceCaps(hdc, HORZRES);
  67.     cyScreen = GetDeviceCaps(hdc, VERTRES);
  68.     ReleaseDC(hwndChild, hdc);
  69.  
  70.     // Calculate new X position, then adjust for screen
  71.     xNew = rcParent.left + ((cxParent - cxChild) / 2);
  72.     if (xNew < 0)
  73.     {
  74.         xNew = 0;
  75.     }
  76.     else if ((xNew + cxChild) > cxScreen)
  77.     {
  78.         xNew = cxScreen - cxChild;
  79.     }
  80.  
  81.     // Calculate new Y position, then adjust for screen
  82.     yNew = rcParent.top  + ((cyParent - cyChild) / 2);
  83.     if (yNew < 0)
  84.     {
  85.         yNew = 0;
  86.     }
  87.     else if ((yNew + cyChild) > cyScreen)
  88.     {
  89.         yNew = cyScreen - cyChild;
  90.     }
  91.  
  92.     // Set it, and return
  93.     return SetWindowPos(hwndChild,
  94.                         NULL,
  95.                         xNew, yNew,
  96.                         0, 0,
  97.                         SWP_NOSIZE | SWP_NOZORDER);
  98. }
  99.  
  100.  
  101. //
  102. //  FUNCTION: CmdStub(HWND, WORD, WORD, HWND)
  103. //
  104. //  PURPOSE:  Display statusbar updates by calling UpdateStatusBar
  105. //
  106. //  PARAMETERS:
  107. //    hwnd     - The window.
  108. //    wCommand - Menu command ID
  109. //    wNotify  - Notification number (unused)
  110. //    hwndCtrl - NULL (unused)
  111. //
  112. //  RETURN VALUE:
  113. //    Always returns 0 - command handled.
  114. //
  115. //  COMMENTS:
  116. //    Assumes there is a resource string describing this command with the
  117. //    same ID as the command ID.  Loads the string and calls UpdateStatusBar
  118. //    to put the string into main pane of the status bar.
  119. //
  120.  
  121. #pragma argsused
  122. LRESULT CmdStub(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  123. {
  124.     char szBuffer[50];   
  125.     int  cbWritten;
  126.  
  127.     cbWritten = LoadString(hInst, wCommand, szBuffer, sizeof(szBuffer)); 
  128.     if(cbWritten == 0) 
  129.     {
  130.         lstrcpy(szBuffer, "Unknown Command");
  131.         UpdateStatusBar(szBuffer, 0, 0);
  132.     }
  133.     else
  134.     { 
  135.         UpdateStatusBar(szBuffer, 0, 0);
  136.         MessageBox (hwnd, 
  137.                     "Command Not Implemented:\r\n Demonstration Purposes Only", 
  138.                     "BarSDI",
  139.                     MB_OK | MB_ICONEXCLAMATION);
  140.     }
  141.      /*
  142.       * Once the command is executed, set the statusbar text to 
  143.       * original text.
  144.       */
  145.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  146.  
  147.     return 0;
  148. }
  149.